package com.kakaxi.mybilibili.module.home.live.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.LiveAllCategoryRecyAdapter;
import com.kakaxi.mybilibili.base.NavigationActivity;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.bean.LiveAreaCategoryItemBean;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 直播全部分类活动类
 * Created by kakaxi on 16/11/14.
 */

public class LiveAllCategoryActivity extends NavigationActivity {

    /** 列表控件 **/
    @BindView(R.id.recv_common_activity)
    RecyclerView mLiveAllCategoryRecyView;

    /** 刷新控件 **/
    @BindView(R.id.srl_common_activity)
    SwipeRefreshLayout mLiveAllCategorySRLayout;

    /** 默认的上边距 **/
    @BindDimen(R.dimen.default_distance)
    int paddingTop;

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_common_recycler;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        showBackBtn();

        // 取消刷新功能
        mLiveAllCategorySRLayout.setEnabled(false);
        // 初始化列表控件
        mLiveAllCategoryRecyView.setClipToPadding(false);
        mLiveAllCategoryRecyView.setPadding(0, paddingTop, 0, 0);
        mLiveAllCategoryRecyView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int
                    position) {
                LiveAreaCategoryItemBean areaCategoryItemBean = (LiveAreaCategoryItemBean)
                        baseQuickAdapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putString(LiveCategoryRoomsActivity.EXTRA_TITLE, areaCategoryItemBean
                        .getName());
                bundle.putInt(LiveCategoryRoomsActivity.EXTRA_AREA_CATEGORY_ID,
                        areaCategoryItemBean.getId());
                goActivity(LiveCategoryRoomsActivity.class, bundle);
            }
        });
    }

    @Override
    protected void initData() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 从上一个页面通过事件巴士传数据过来
     *
     * @param object 事件巴士对象
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onShowDataEventBus(EventBusObject object) {
        if (object.getEvent() == EventBusObject.EVENT_LIVE_ALL_CATEGORY_ACT_SHOW_DATA) {
            List<LiveAreaCategoryItemBean> areaCategoryItemBeenList = CastUncheckedUtil.cast
                    (object.getData());
            if (areaCategoryItemBeenList.size() != 0) {
                mLiveAllCategoryRecyView.setAdapter(new LiveAllCategoryRecyAdapter
                        (areaCategoryItemBeenList));
            } else {
                // 空白界面
            }
        }
    }


}
